#!/bin/bash
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPTDIR"

if (($EUID!=0));then
    #非root用户的时候,需要检测是否有sudo命令,如果有sudo命令则还要检测当前用户是否可以使用sudo命令
    #因为下面有的指令需要root权限
    if command -v sudo >/dev/null 2>&1;then
        sudo true || { echo "Error: Current user cannot use command 'sudo' !";exit 1; }
    else
        echo "Error: Current user is not root,and can not find sudo cmd!"
        exit 1
    fi
fi
case $(uname) in
    Darwin)
        OS="mac"
        ;;
    Linux)
        if command -v pacman >/dev/null 2>&1;then
            OS="arch"
        elif command -v dnf >/dev/null 2>&1;then
            OS="fedora"
        elif command -v apt-get >/dev/null 2>&1;then
            if lsb_release -a 2>/dev/null |grep Debian >/dev/null 2>&1;then
                OS="debian"
            fi
            if lsb_release -a 2>/dev/null |grep Ubuntu >/dev/null 2>&1;then
                OS="ubuntu"
            fi
        fi
        ;;
    *)
        echo "Not support your OS!"
        exit 1
esac

user=${SUDO_USER:-$(whoami)}
setSupervim=y
setGit=y
setShell=y

setDocker=n
setGolang=n
# read -p "Set git? [Y/n] " setGit
#read -p "Set docker? [y/N] (only support arch)" setDocker
#read -p "Set golang? [y/N] " setGolang
# read -p "Set shell? [Y/n] (install zsh,zsh theme,source .custom-shell) " setShell
# read -p "Set supervim? [Y/n] " setSupervim
##Install dev tools
if (($EUID!=0));then
    echo "You are not running the script by root privilege."
    echo "Try to use sudo..."
    sudo /bin/bash dev/dev-$OS
else
    /bin/bash dev/dev-$OS
fi

if (($?!=0));then
    echo "Run dev-$OS failed!"
    exit 1
fi
read -p "set iptables service (for systemd)? [Y/n] " setIp
if [[ "$setIp" != [nN] ]];then
    if (($EUID!=0));then
        sudo /bin/bash service/iptablesService/install.sh
    else
        /bin/bash service/iptablesService/install.sh
    fi
fi

##Set git
if [[ "$setGit" != [nN] ]];then
    echo "Config git..."
    /bin/bash git/setGit
fi

##Set docker
if [[ "$setDocker" == [yY] ]];then
    case "$OS" in
        "arch")
            if (($EUID!=0));then
                sudo /bin/bash docker/docker-arch install
            else
                /bin/bash docker/docker-arch install
            fi
            ;;
    esac
fi

##Set golang
if [[ "$setGolang" == [yY] ]];then
    if ! command -v go >/dev/null 2>&1;then
        read -p "Cannot find go,install it manaually? [Y/n] " insGo
        if [[ $insGo != [nN] ]];then
            read -p "Install golang manaually,input socks5 proxy for curl:(empty for not use proxy) " sp
            if (($EUID!=0));then
                sudo bash ./golang/installGo.sh -p "$sp"
            else
                bash ./golang/installGo.sh -p "$sp"
            fi
        fi
    fi
    GOPATH=$HOME/go
    if [ ! -d $GOPATH ];then
        mkdir -pv $GOPATH/src
    fi
fi

##Set shell
if [[ "$setShell" != [nN] ]];then
    echo "Install zsh and oh-my-zsh..."
    /bin/bash shell/10-zsh-installer.sh
    /bin/bash shell/20-set-zsh-theme.sh
    /bin/bash shell/30-config-shell.sh install
fi

##Set vim
if [[ "$setSupervim" != [nN] ]];then
    echo "Install supervim with vim-go and YouCompleteMe..."
    cd supervim
    case "$OS" in
        Arch|Darwin)
            /bin/bash install nvim
            ;;
        *)
            /bin/bash install vim
            ;;
    esac
    cd -
    echo "Done."
fi

# vim: set filetype=sh
